package com.UnionPayCenter.shop.impl;

import com.UnionPayCenter.entity.ShopEntity;
import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.ShopMapper;
import com.UnionPayCenter.shop.ShopManager;
import com.UnionPayCenter.shop.domain.ShopMerchant;
import com.UnionPayCenter.vastchain.VastChainService;
import com.UnionPayCenter.vastchain.enums.MerchantTypeEnum;
import com.UnionPayCenter.vastchain.request.ParentMerchantRequest;
import com.UnionPayCenter.vastchain.request.PaymentRequest;
import com.UnionPayCenter.vastchain.request.SubMerchantRequest;
import com.UnionPayCenter.vastchain.request.params.ParentMerchantParameter;
import com.UnionPayCenter.vastchain.request.params.PaymentParameter;
import com.UnionPayCenter.vastchain.request.params.SubMerchantParameter;
import com.UnionPayCenter.vastchain.response.params.ParentMerchantResponseParams;
import com.UnionPayCenter.vastchain.response.params.SubMerchantResponseParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @author noah
 * @since 1.0.0, 2019/08/21
 */
@Service
public class ShopManagerImpl implements ShopManager {

    @Autowired
    private VastChainService vastChainService;

    @Autowired
    private ShopMapper shopMapper;


    @Override
    public ShopMerchant getShopMerchant(String shopName, String bizMchId, String terminalId) {

        ParentMerchantRequest parentMerchantRequest = new ParentMerchantRequest();
        ParentMerchantParameter parentMerchantParameter = new ParentMerchantParameter();
        parentMerchantParameter.setDisplayName(shopName);
        parentMerchantRequest.setParameters(parentMerchantParameter);
        parentMerchantRequest.setType(MerchantTypeEnum.merchant);

        ParentMerchantResponseParams parentMerchantResponse = vastChainService.creatParentMerchant(parentMerchantRequest);

        if(Objects.isNull(parentMerchantResponse)){
            throw  new ApiException(ErrorCodeEnum.SC2);
        }
        SubMerchantRequest subMerchantRequest = new SubMerchantRequest();
        SubMerchantParameter subMerchantParameter = new SubMerchantParameter();
        subMerchantParameter.setParentMerchantId(parentMerchantResponse.getId());
        subMerchantParameter.setDisplayName(shopName);
        subMerchantRequest.setType(MerchantTypeEnum.subMerchant);
        subMerchantRequest.setParameters(subMerchantParameter);

        SubMerchantResponseParams subResponse= vastChainService.creatSubMerchant(subMerchantRequest);
        if(Objects.isNull(subResponse)){
            throw new ApiException(ErrorCodeEnum.SC2);
        }
        PaymentRequest paymentRequest = new PaymentRequest();
        PaymentParameter paymentParameter = new PaymentParameter();
        paymentParameter.setUnionPayBizMchId(bizMchId);
        paymentParameter.setTerminalId(terminalId);
        paymentRequest.setParameters(paymentParameter);
        paymentRequest.setId(subResponse.getId());
        boolean rs =  vastChainService.modifyPaymentParams(paymentRequest);
        if(!rs){
            throw new ApiException(ErrorCodeEnum.SC2);
        }

        ShopMerchant shopMerchant = new ShopMerchant();
        shopMerchant.setParentMerchant(parentMerchantResponse.getId());
        shopMerchant.setSubMerchant(subResponse.getId());
        shopMerchant.setBizMchId(bizMchId);
        shopMerchant.setTerminalId(terminalId);
        shopMerchant.setPaymentTag("1");

        return shopMerchant;
    }

    @Override
    public ShopMerchant getShopMerchantInfo(ShopInfoEntity shopInfoEntity, String bizMchId, String terminalId) {
        if(Objects.isNull(shopInfoEntity) || StringUtils.isAnyBlank(bizMchId,terminalId)){
            return null;
        }
        if(bizMchId.equals(shopInfoEntity.getBizMachId()) && terminalId.equals(shopInfoEntity.getYlTid())
                && "1".equals(shopInfoEntity.getYlPaymentTag())){
            ShopMerchant shopMerchant = new ShopMerchant();
            shopMerchant.setTerminalId(terminalId);
            shopMerchant.setBizMchId(shopInfoEntity.getBizMachId());
            shopMerchant.setSubMerchant(shopInfoEntity.getYlMerchant());
            shopMerchant.setParentMerchant(shopInfoEntity.getYlParentMerchant());
            shopMerchant.setPaymentTag(shopInfoEntity.getYlPaymentTag());
            return shopMerchant;
        }
        ShopEntity shopEntity = shopMapper.selectByShopId(shopInfoEntity.getShopId());
        if(Objects.isNull(shopEntity) || StringUtils.isBlank(shopEntity.getShopName())){
            throw new ApiException(ErrorCodeEnum.SHOP_NOT_EXISTS);
        }
        return getShopMerchant(shopEntity.getShopName(),bizMchId,terminalId);
    }
}
